<html>
<title>集合竞价</title>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
<script src="{{ url_for('static',filename='js/data.js') }}"></script>
<script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>

<body>
    <div class="table-one" align="center">
        <table id="mytable" cellspacing="0"></table>
    </div>
</body>

<script type="text/javascript">
    var ID_THREAD = -1; //循环线程
    var tb = document.getElementById("mytable"); //获取表格的dom节点
    $(document).ready(function() {
        get_jihejingjia_limit()
        auto_update();
    });

    function auto_update() {
        data = get_server_data();
        if (get_date() == data.day) {
            ID_THREAD = setInterval('is_running()', 1000 * 3); //指定3秒刷新一次
        } else {
            if (ID_THREAD != -1) {
                clearInterval(ID_THREAD)
            }
        }
    }

    function get_server_data() {
        var m_data = []
        $.ajax({
            url: '/test',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function get_date() {
        //获取当前日期
        var d = new Date();
        var m_date = d.getFullYear() + "-" + formatZero(String(parseInt(d.getMonth()) + 1)) + "-" + formatZero(d.getDate());
        return m_date;
    }

    //格式化日期时间
    function formatZero(n) {
        if (n >= 0 && n <= 9) {
            return "0" + n;
        } else {
            return n;
        }
    }

    function get_time() {
        //获取当前时间
        var d = new Date();
        var m_date = d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
        // return m_date;
        // 判断时间是否在开盘时间段
        if ((15 * 60 * 60) > (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) && (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) > (9 * 60 * 60 + 15 * 60)) {
            return true
        } else {
            return false
        }
    }

    function is_running() {
        if (get_time()) {
            ret = get_jihejingjia_limit()
            console.log(ret)
        } else {
            if (ID_THREAD != -1) {
                clearInterval(ID_THREAD)
            }
        }
    }

    // 获取集合竞价数据
    function get_jihejingjia_limit() {
        var m_data = []
        $.ajax({
            url: '/get_jihejingjia_limit',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
                set_table(data)
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function set_table(data) {
        var names = get_now_limit_names()// 获取涨停板股票名称池

        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th style=\"width:68px\">股票名称</th>\
            <th>涨停委买额</th>\
            <th>竞价涨幅</th>\
            <th>实时涨幅</th>\
            <th>状态</th>\
            <th>板块</th>\
            <th>竞价净额</th>\
            <th>竞价成交额</th>\
            <th>竞价换手</th>\
            <th>实际流通</th>\
            </tr>"
        m_list = data['date']['list']
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"
            if(names.indexOf(m_list[i_row][1]) != -1){
                m_html += "<td class=\"rbnews\">" + m_list[i_row][1] + "</td>"
            }else        
                m_html += "<td>" + m_list[i_row][1] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][6]) + "</td>"
                // 设置颜色
            if (m_list[i_row][19] > 0) {
                m_html += "<td class=\"red\">" + m_list[i_row][19].toFixed(2) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + m_list[i_row][19].toFixed(2) + "</td>"
            if (m_list[i_row][4] > 0) {
                m_html += "<td class=\"red\">" + m_list[i_row][4].toFixed(2) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + m_list[i_row][4].toFixed(2) + "</td>"
            m_html += "<td>" + m_list[i_row][9] + "</td>"
            m_html += "<td>" + m_list[i_row][16] + "</td>"
            if (m_list[i_row][20] > 0) {
                m_html += "<td class=\"red\">" + format_money(m_list[i_row][20]) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + format_money(m_list[i_row][20]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][22]) + "</td>"
            m_html += "<td>" + m_list[i_row][21].toFixed(2) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][15]) + "</td>"
            m_html += "</tr>"
        }
        tb.innerHTML = m_th + m_html
    }

    function format_money(money) {
        //金钱转换
        if (money > 0) {
            if (money / 10000 > 10000) {
                return (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return (money / 10000).toFixed(0) + "万"
            }
        } else {
            money = Math.abs(money);
            if (money / 10000 > 10000) {
                return "-" + (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return "-" + (money / 10000).toFixed(0) + "万"
            }
        }
    }
</script>

</html>